<template>
  <div class="param-main">
    <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="100px">
      <el-row :span="24">
        <el-col :span="6">
          <el-form-item label="调度名称">
            <el-input v-model="tblMain.param.applyName" size="small" placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="状态">
            <el-select v-model="tblMain.param.confirmStatus" clearable placeholder="" size="small" style="width: 100%;">
              <el-option v-for="it in confirmList" :key="it.dictItemCode" :label="it.dictItemName"
                :value="it.dictItemCode" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="handleAdd()">新增</el-button>
      </div>
    </el-form>
    <div>
      <el-table ref="tblMain" style="margin-top: 10px;" :data="tblMain.items" v-loading.body="tblMain.loading" border
        stripe>
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="220px">
          <template slot-scope="scope">
            <el-button v-if="scope.row.confirmStatus == '01'" type="text" @click="handleRevoke(scope.row)">撤回</el-button>
            <el-divider v-if="scope.row.confirmStatus == '01'" direction="vertical"></el-divider>
            <el-button v-if="scope.row.confirmStatus == '07'" type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-divider v-if="scope.row.confirmStatus == '07'" direction="vertical"></el-divider>
            <el-button v-if="scope.row.confirmStatus == '07'" type="text" @click="deleteRow(scope.row)">删除</el-button>
            <el-divider v-if="scope.row.confirmStatus == '07'" direction="vertical"></el-divider>
            <el-button v-if="scope.row.confirmStatus == '02'" type="text"
              @click="handleEntered(scope.row, '03')">已入场</el-button>
            <el-divider v-if="scope.row.confirmStatus == '02'" direction="vertical"></el-divider>
            <el-button v-if="scope.row.confirmStatus == '02'" type="text"
              @click="handleEntered(scope.row, '08')">未入场</el-button>
            <el-divider v-if="scope.row.confirmStatus == '02'" direction="vertical"></el-divider>
            <el-button type="text" @click="handleDetail(scope.row)">明细</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="applyName" label="调度名称" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="skillTypeName" label="所需技能名称" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="storeConfirmName" label="被调度门店" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="empName" label="被调度技师" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="dispatchStartTime" label="开始时间" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="dispatchEndTime" label="结束时间" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="状态" align="center" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag v-if="scope.row.confirmStatus == '01'" size="small">未确认</el-tag>
            <el-tag v-if="scope.row.confirmStatus == '02'" size="small">已确认</el-tag>
            <el-tag v-if="scope.row.confirmStatus == '03'" size="small">已入场</el-tag>
            <el-tag v-if="scope.row.confirmStatus == '04'" size="small" type="success">已完成</el-tag>
            <el-tag v-if="scope.row.confirmStatus == '05'" size="small" type="danger">已驳回</el-tag>
            <el-tag v-if="scope.row.confirmStatus == '06'" size="small" type="warning">已超期</el-tag>
            <el-tag v-if="scope.row.confirmStatus == '07'" size="small" type="info">已撤回</el-tag>
            <el-tag v-if="scope.row.confirmStatus == '08'" size="small" type="danger">未入场</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="margin-top: 10px" :current-page.sync="tblMain.currentPage" :page-size="tblMain.pageSize"
        :total="tblMain.total" layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
    </div>
    <technician-dialog ref="modalForm" @ok="modalFormOk"></technician-dialog>
    <technician-approval-dialog ref="modalDetail"></technician-approval-dialog>
  </div>
</template>
<script>
import TechnicianDialog from './components/TechnicianDialog'
import TechnicianApprovalDialog from './components/TechnicianApprovalDialog'
import tool from '@/tool'
import {
  getDictionaryItemMap
} from '@/api/common'
import {
  technicianDispatchList,
  technicianDispatchUpdateStatus,
  technicianDispatchDel
} from '@/api/business'
export default ({
  name: 'TechnicianList',
  components: {
    TechnicianDialog,
    TechnicianApprovalDialog
  },
  data() {
    return {
      tblMain: tool.genTableData({
        param: {
          applyName: '',
          confirmStatus: ''
        },
        items: []
      }),
      confirmList: []
    }
  },
  mounted() {
    this.init();
    this.initTableData();
  },
  methods: {
    init() {
      getDictionaryItemMap({ codeArr: "confirmStatus" }).then(res => {
        if (res.data.code == 0) {
          this.confirmList = res.data.data.confirmStatus
        }
      })
    },
    modalFormOk() {
      this.initTableData();
    },
    handleAdd() {
      this.$refs.modalForm.add();
    },
    handleEdit(val) {
      this.$refs.modalForm.edit(val);
    },
    handleDetail(val) {
      this.$refs.modalDetail.edit(val);
    },
    async handleRevoke(val) {
      this.$confirm('确定撤回该申请?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = {
          id: val.id,
          confirmStatus: "07"
        }
        await this.updateStatus(params, "撤回成功")
      }).catch(() => {
      })
    },
    async handleEntered(val, flag) {
      let msg = null;
      if (flag == "03") {
        msg = "确定该技师已按时入场？"
      } else {
        msg = "确定该技师未按时入场？"
      }
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = {
          id: val.id,
          confirmStatus: flag
        }
        await this.updateStatus(params, "操作成功")
      }).catch(() => {
      })
    },
    // 更新状态
    updateStatus(params, msg) {
      let me = this
      me.tblMain.loading = true
      technicianDispatchUpdateStatus(params).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.$message.success(msg)
          this.initTableData()
        }
      }).catch(() => {
        me.$message.error('请求失败')
        me.tblMain.loading = false
      })
    },
    // 单个删除
    async deleteRow(val) {
      this.$confirm('确定删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let ids = []
        ids.push(val.id)
        await this.deleteTblMain({ ids: ids })
      }).catch(() => {
      })
    },
    // 删除数据
    deleteTblMain(params) {
      let me = this
      me.tblMain.loading = true
      technicianDispatchDel(params).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.$message.success('删除成功')
          this.initTableData()
        }
      }).catch(() => {
        me.$message.error('请求失败')
        me.tblMain.loading = false
      })
    },
    dataReset() {
      this.tblMain.currentPage = 1
      this.tblMain.pageSize = 10
      this.tblMain.param.applyName = ''
      this.tblMain.param.confirmStatus = ''
      this.initTableData()
    },
    handleSizeChange(val) {
      this.tblMain.pageSize = val
      this.initTableData()
    },
    handleCurrentChange(val) {
      this.tblMain.currentPage = val
      this.initTableData()
    },
    initTableData() {
      let me = this
      let param = {
        applyName: me.tblMain.param.applyName,
        confirmStatus: me.tblMain.param.confirmStatus,
        page: me.tblMain.currentPage,
        size: me.tblMain.pageSize,
        type: "0"
      }
      me.tblMain.loading = true
      technicianDispatchList(param).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.tblMain.items = data.data.records
          me.tblMain.total = data.data.total
        }
      })
    },
  }
})
</script>
